package com.coocaa.ops.admin.v1.globalparams;

import com.coocaa.ops.admin.base.annotation.Auth;
import com.coocaa.ops.admin.base.annotation.Config;
import com.coocaa.ops.admin.base.controller.MgrBaseController;
import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.base.util.WebUtil;
import com.coocaa.ops.admin.v1.auth.sys.constant.MenuTokenConstants;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import com.coocaa.ops.admin.v1.globalparams.model.GlobalParamsModel;
import com.coocaa.ops.admin.v1.globalparams.model.GlobalParamsPageListModel;
import com.coocaa.ops.admin.v1.globalparams.service.IGlobalParamsService;
import com.coocaa.ops.common.core.base.enums.AuthEnum;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.tool.json.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author : hql
 * @date : 2020/7/14 10:44
 * @description
 */
@Config(menuToken = MenuTokenConstants.GLOBAL_PARAMS)
@RestController
@RequestMapping(value = "/v1/globalParams")
public class GlobalParamsController extends MgrBaseController {

    @Autowired
    private IGlobalParamsService globalParamsService;

    private static CommonLanguageProperties commonLanguageProperties;

    @Auth(verifyAuthority = true, authorityType = AuthEnum.SAVE)
    @RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
    public Object save(HttpServletRequest request, @RequestParam String jsonStr) {
        SysUserEntity userInfo = WebUtil.getCurrentUser(request);
        GlobalParamsModel model = (GlobalParamsModel) JsonUtil.json2Object(jsonStr, GlobalParamsModel.class);
        globalParamsService.save(model, userInfo.getId());
        return R.ok();
    }

    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/pageList", produces = "text/html;charset=UTF-8")
    public Object pageList(HttpServletRequest request, GlobalParamsModel model) {
        PageResultModel<GlobalParamsPageListModel> result = globalParamsService.getPageList(model);
        return R.ok(result);
    }

    @Auth(verifyAuthority = true, authorityType = AuthEnum.DELETE)
    @RequestMapping(value = "/remove", produces = "text/html;charset=UTF-8")
    public Object deleteBatch(HttpServletRequest request, @RequestParam("id") List<Integer> ids) {
        Assert.notEmpty(ids, "id" + commonLanguageProperties.getNotNull());
        return globalParamsService.deleteBatch(ids, WebUtil.getCurrentUser(request));
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        GlobalParamsController.commonLanguageProperties = commonLanguageProperties;
    }

    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getMenuInfo", produces = "text/html;charset=UTF-8")
    public Object getMenuInfo(HttpServletRequest request) {
        Map result = getMenuInfo(this.getClass(), request);
        return render2Json(result);
    }


}
